package com.qumitech.visualnovel.service.voiceRoom.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.voiceRoom.VoiceRoomVo;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.voiceroom.TopVoiceRoomDTO;
import com.qumitech.visualnovel.pubdef.dto.voiceroom.VoiceRoomSearchDTO;
import com.qumitech.visualnovel.service.voiceRoom.VoiceRoomConfigService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings("unchecked")
public class VoiceRoomConfigServiceImpl implements VoiceRoomConfigService {

    @Override
    public void updateVoiceRoomCfg(VoiceRoomVo voiceRoomVo) {
        String url = UrlConstant.SERVER_URL + VOICEROOM_CONFIG_URL;
        Integer index = voiceRoomVo.getIndex();
        String label = index != null ? voiceRoomVo.getLable() + index : null;
        voiceRoomVo.setLable(label);
        String req = JSONObject.toJSONString(voiceRoomVo);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public Map<String, Object> getVoiceRoom(Byte roomType, Byte orderBy, Integer offset, Integer limit) {
        String url = UrlConstant.SERVER_URL + VOICEROOM_CONFIG_ADMIN_URL + "/roomlist";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("roomType", roomType.toString());
        paramMap.put("orderBy", orderBy.toString());
        paramMap.put("page", offset.toString());
        paramMap.put("pageSize", limit.toString());
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("uid", "1");
        String body = HttpInvokeUtil.httpGet(url, paramMap, headerMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        if (rspMap.get("room_list") == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rspMap;
    }

    @Override
    public Object getTopVoiceRoom(VoiceRoomSearchDTO searchDTO) {
        String url = UrlConstant.SERVER_URL + VOICEROOM_CONFIG_ADMIN_URL + VOICEROOM_TOP;
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(searchDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp.getData();
    }

    @Override
    public BaseRsp addTopVoiceRoom(TopVoiceRoomDTO topRoomConfig) {
        String url = UrlConstant.SERVER_URL + VOICEROOM_CONFIG_URL + VOICEROOM_TOP;
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        topRoomConfig.setOperator(activeUser.getUsername());
        String req = JSONObject.toJSONString(topRoomConfig);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);

    }

    @Override
    public BaseRsp updateTopVoiceRoom(Long configId, TopVoiceRoomDTO topRoomCfg) {
        String url = UrlConstant.SERVER_URL + VOICEROOM_CONFIG_URL + VOICEROOM_TOP + "/" + configId;
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        topRoomCfg.setOperator(activeUser.getUsername());
        String req = JSONObject.toJSONString(topRoomCfg);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);

    }

    @Override
    public BaseRsp deleteTopVoiceRoom(Long configId) {
        String url = UrlConstant.SERVER_URL + VOICEROOM_CONFIG_URL + VOICEROOM_TOP + "/" + configId;
        String body = HttpInvokeUtil.httpDelete(url, null);
        return JSONObject.parseObject(body, BaseRsp.class);

    }

    @Override
    public Map<String, Object> getVoiceRoomList(VoiceRoomSearchDTO searchDTO) {
        String url = UrlConstant.SERVER_URL + VOICEROOM_CONFIG_ADMIN_URL + VOICEROOM_LIST;
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("query", searchDTO.getQuery());
        paramMap.put("limit", searchDTO.getLimit().toString());
        paramMap.put("offset", searchDTO.getOffset().toString());
        Map<String, String> headerMap = new HashMap<>();
        String body = HttpInvokeUtil.httpGet(url, paramMap, headerMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        if (rspMap.get("room_list") == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rspMap;
    }
}
